package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author ：qcq
 * @date ：Created in 2020/12/5 9:17
 * @description：
 */
@Service
@Transactional
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {


    @Override
    public PageDTO findPage(String key, Integer page, Integer rows) {
        if (page == 0) {
            page = 1;
        }
        if (rows == 0) {
            rows = 10;
        }
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        PageDTO result = null;
        if (!StringUtils.isBlank(key)) {
            queryWrapper.like("name", key).or().eq("letter",key);
            Page<Brand> brandPage = page(new Page<Brand>(page, rows), queryWrapper);
            Long totalPage = (long) (Math.ceil(brandPage.getTotal() / rows));
            result = new PageDTO(brandPage.getTotal(), totalPage/*brandPage.getPages(),*/, brandPage.getRecords());
        } else {
            Page<Brand> brandPage = page(new Page<Brand>(page, rows));
            double num =(Math.ceil((double) brandPage.getTotal() / rows));
            Long totalPage = (long) num;
            result = new PageDTO(brandPage.getTotal(), totalPage, brandPage.getRecords());
        }
        /*IPage<Brand> iPage = new Page<>(page, rows);
        boolean isCondition = StringUtils.isNotBlank(key);
        page(iPage, new QueryWrapper<Brand>()
                .like(isCondition, "name", key).or().eq(isCondition, "letter", key));*/

        return result;
    }

    @Override
    @Transactional
    public boolean saveBrand(BrandDTO brandDto) {

        return addOrUpdateBrand(brandDto);
    }

    @Override
    @Transactional
    public boolean deleteBrandById(Long id) {

        boolean flag = this.removeById(id);
        baseMapper.deleteCategoryBrand(id);
        return flag;
    }

    @Override
    @Transactional
    public Boolean updateBrand(BrandDTO brandDto) {

        Long id = brandDto.getId();
        /*if (StringUtils.isNotBlank(String.valueOf(id))) {
            baseMapper.deleteCategoryBrand(id);
        }*/
        baseMapper.deleteCategoryBrand(id);
        return addOrUpdateBrand(brandDto);
    }


    private Boolean addOrUpdateBrand(BrandDTO brandDto) {
        List<Integer> categoryIds = brandDto.getCategoryIds();
        Brand brand = brandDto.toEntity(Brand.class);
        boolean flag = this.saveOrUpdate(brand);
        Long brandId = brand.getId();
        boolean flag1 = false;
        if (categoryIds != null && categoryIds.size() > 0) {
            for (Integer categoryId : categoryIds) {
                flag1 = baseMapper.addCategoryBrand(categoryId, brandId);
            }
        }
        return (flag && flag1);
    }

    @Override
    public List<BrandDTO> queryBrandListByIds(List<Long> ids) {
        List<Brand> brandList = this.listByIds(ids);
        List<BrandDTO> brandDtoList = BrandDTO.convertEntityList(brandList);
        return brandDtoList;
    }

    @Override
    public BrandDTO queryBrandById(Long id) {
        Brand brand = baseMapper.selectById(id);
        BrandDTO brandDto = new BrandDTO(brand);
        return brandDto;
    }

    @Override
    public List<BrandDTO> queryBrandByCategoryId(Long id) {
        List<BrandDTO> brandDtoList = baseMapper.queryBrandByCategoryId(id);
        return brandDtoList;
    }
}
